package com.manage.service.impl;

import com.manage.dao.IDeptDao;
import com.manage.dao.IEmpDao;
import com.manage.domain.Dept;
import com.manage.service.IDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class DeptServiceImpl implements IDeptService {

    @Autowired
    private IDeptDao deptDao;
    @Autowired
    private IEmpDao empDao;

    @Override
    public List<Dept> list() {
        return deptDao.list();
    }

    @Override
    public List<Dept> listByName(String name) {
        return deptDao.listByName(name);
    }

    @Override
    public void add(Dept dept) {
        deptDao.insert(dept);
    }

    @Override
    public void update(Dept dept) {
        deptDao.update(dept);
    }

    @Override
    public void delete(Integer id) {
        int cnt = empDao.countByDeptId(id);
        if (cnt > 0) {
            throw new IllegalStateException("该部门下存在员工，无法删除");
        }
        deptDao.deleteById(id);
    }
}


